package test;

/*
 *
 * FileInputDemo
 * Demonstrates the FileInputStream class
 * and DataInputStream
 */
import java.io.*;

class FileInputDemo 
{
	public static void main(String args[])
	{
		// args.length is equivalent to argc in C
		if (args.length == 1)
		{
			try
			{
				// check if file exists
				File file = new File(args[0]);
				System.out.println(file + (file.exists()? " is found " : " is missing "));
				
				if(file.exists())
				{
					// Open the file that is the first 
					// command line parameter
					FileInputStream fstream = new FileInputStream(args[0]);
					
					// Convert our input stream to a
					// DataInputStream
					DataInputStream in = new DataInputStream(fstream);
					
					// Continue to read lines while 
					// there are still some left to read
					while (in.available() !=0)
					{
						// Print file line to screen
						BufferedReader d = new BufferedReader(new InputStreamReader(in));
						System.out.println(d);
					}
					
					in.close();
				}
				else
				{
					System.err.println("File does not exist.");
				}
			} 
			catch (Exception e)
			{
				System.err.println("File input error");
			}
		}
		else
		{
			System.out.println("Invalid parameters");
		}
	}
}